home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume10 / chall < prev    next >
Encoding:
Text File  |  1990-01-19  |  11.4 KB  |  447 lines

  1. Newsgroups: comp.sources.misc
  2. organization: APT Technology, Inc.  San Jose, CA,  USA
  3. subject: v10i025: chall source program submittal
  4. from: brian@apt.UUCP (Brian Litzinger)
  5. Sender: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  6.  
  7. Posting-number: Volume 10, Issue 25
  8. Submitted-by: brian@apt.UUCP (Brian Litzinger)
  9. Archive-name: chall
  10.  
  11. [And again:  ???  ++bsa]
  12.  
  13. The following program is just a useless waste of your time, so I'd
  14. just /bin/rm it and go one with more useful endeavours.
  15.  
  16. I've been accused of posting useless programs in the past, so I think
  17. you should seriously re-consider just tossing this code out.
  18.  
  19. There really are better ways to do these things.  This whole mess
  20. is just a waste of network bandwidth and I'm surprised the moderator
  21. actually let it slip through.
  22.  
  23. If you still want to use this program its a program using getopts that
  24. lets you combine chmod, chown, and chgrp into one command called chall.
  25.  
  26. It doesn't follow any of the BSD combined chown/grp functionality. I'm
  27. a System V'er after all.  It does have one saving grace though, you
  28. can maintain a personal .chall file in your home directory with macros
  29. in it like 'man:bin:bin:444' which means you can say 
  30.     chall -x man foobar
  31. and foobar will be chmod 444 foobar, chown bin foobar, chgrp bin foobar.
  32.  
  33. See the man page for more information.  It's too bad it doesn't 
  34. recursively decend directories, and it's too bad it doesn't use
  35. the +rw ... stuff of BSD.  The latter I actually tried, but all those
  36. different options, there just didn't seem to be away to do it.
  37.  
  38. <>  Brian Litzinger @ APT Technology Inc., San Jose, CA
  39. <>  UUCP:  {apple,sun,pyramid}!daver!apt!brian    brian@apt.UUCP
  40. <>  VOICE: 408 370 9077      FAX: 408 370 9291
  41.  
  42. #! /bin/sh
  43. # This is a shell archive.  Remove anything before this line, then unpack
  44. # it by saving it into a file and typing "sh file".  To overwrite existing
  45. # files, type "sh file -c".  You can also feed this as standard input via
  46. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  47. # will see the following message at the end:
  48. #        "End of shell archive."
  49. # Contents:  README chall.c chall.1
  50. # Wrapped by brian@apt on Wed Jan 17 00:12:56 1990
  51. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  52. if test -f 'README' -a "${1}" != "-c" ; then 
  53.   echo shar: Will not clobber existing file \"'README'\"
  54. else
  55. echo shar: Extracting \"'README'\" \(1300 characters\)
  56. sed "s/^X//" >'README' <<'END_OF_FILE'
  57. Xchall
  58. X
  59. XBy Brian E. Litzinger
  60. X
  61. XYou can do what you wish with this code.  Just don't make a profit
  62. Xoff of it.
  63. X
  64. XThe following program is just a useless waste of your time, so I'd
  65. Xjust /bin/rm it and go one with more useful endeavours.
  66. X
  67. XI've been accused of posting useless programs in the past, so I think
  68. Xyou should seriously re-consider just tossing this code out.
  69. X
  70. XThere really are better ways to do these things.  This whole mess
  71. Xis just a waste of network bandwidth and I'm surprised the moderator
  72. Xactually let it slip through.
  73. X
  74. XIf you still want to use this program its a program using getopts that
  75. Xlets you combine chmod, chown, and chgrp into one command called chall.
  76. X
  77. XIt doesn't follow any of the BSD combined chown/grp functionality. I'm
  78. Xa System V'er after all.  It does have one saving grace though, you
  79. Xcan maintain a personal .chall file in your home directory with macros
  80. Xin it like 'man:bin:bin:444' which means you can say 
  81. X    chall -x man foobar
  82. Xand foobar will be chmod 444 foobar, chown bin foobar, chgrp bin foobar.
  83. X
  84. XSee the man page for more information.  It's too bad it doesn't 
  85. Xrecursively decend directories, and it's too bad it doesn't use
  86. Xthe +rw ... stuff of BSD.  The latter I actually tried, but all those
  87. Xdifferent options, there just didn't seem to be away to do it.
  88. X
  89. END_OF_FILE
  90. if test 1300 -ne `wc -c <'README'`; then
  91.     echo shar: \"'README'\" unpacked with wrong size!
  92. fi
  93. # end of 'README'
  94. fi
  95. if test -f 'chall.c' -a "${1}" != "-c" ; then 
  96.   echo shar: Will not clobber existing file \"'chall.c'\"
  97. else
  98. echo shar: Extracting \"'chall.c'\" \(4143 characters\)
  99. sed "s/^X//" >'chall.c' <<'END_OF_FILE'
  100. X/* $Header: chall.c,v 1.4 89/12/18 18:39:55 brian Locked $ */
  101. X
  102. X/* By Brian E. Litzinger */
  103. X
  104. X#include <stdio.h>
  105. X#include <unistd.h>
  106. X#include <pwd.h>
  107. X#include <grp.h>
  108. X#include <sys/types.h>
  109. X#include <sys/stat.h>
  110. X#include <string.h>
  111. X
  112. Xmain(argc,argv)
  113. Xint argc;
  114. Xchar **argv;
  115. X{
  116. X    int c;
  117. X    extern char *optarg;
  118. X    extern int optind;
  119. X    extern int errno;
  120. X
  121. X    struct stat *malloc();
  122. X
  123. X    struct passwd *p;
  124. X    struct passwd *getpwnam();
  125. X
  126. X    struct group *g;
  127. X    struct group *getgrnam();
  128. X
  129. X    struct stat *f;
  130. X    int stat();
  131. X
  132. X    char t[256];
  133. X    int tlen;
  134. X
  135. X    int cmask;
  136. X
  137. X    FILE *fd;
  138. X
  139. X    char buffer[100];
  140. X
  141. X    char *s;
  142. X    char *q;
  143. X
  144. X    char *home;
  145. X
  146. X    short matchfound;
  147. X
  148. X    short errflag;
  149. X    short dflag;
  150. X    short oflag;
  151. X    short mflag;
  152. X    short gflag;
  153. X    short sflag;
  154. X    short xflag;
  155. X
  156. X    char *directory;
  157. X    char *owner;
  158. X    int Vowner;
  159. X    int OLDowner;
  160. X    char *group;
  161. X    int Vgroup;
  162. X    int OLDgroup;
  163. X    int mode;
  164. X
  165. X    errflag=gflag=mflag=oflag=dflag=xflag=0;
  166. X
  167. X    home=(char *)getenv("HOME");
  168. X
  169. X    /*
  170. X    +r -r +w -w +x -x +u -u +g -g
  171. X    umask((cmask=umask(0));
  172. X    */
  173. X
  174. X    while ((c=getopt(argc,argv,"x:o:u:g:m:d:")) != -1)
  175. X    switch (c) {
  176. X        case 'x':
  177. X        if (!home) {
  178. X            fprintf(stderr,"%s: no HOME variable in environment.",
  179. X                argv[0]);
  180. X            fprintf(stderr,"%s: Unable to file chall library file",
  181. X                argv[0]);
  182. X            exit(2);
  183. X        }
  184. X        matchfound = 0;
  185. X        sprintf(buffer,"%s/.chall",home);
  186. X        if((fd=fopen(buffer,"r"))!=NULL) {
  187. X            while ( !matchfound && fgets(buffer,sizeof(buffer),fd)) {
  188. X            q = strtok(buffer,":");
  189. X            if (q && !strcmp(optarg,q)) {
  190. X                matchfound = -1;
  191. X                if (q=strtok(NULL,":"))
  192. X                if (!oflag) {
  193. X                    owner = q;
  194. X                    oflag++;
  195. X                }
  196. X                if (q=strtok(NULL,":"))
  197. X                if (!gflag) {
  198. X                    group = q;
  199. X                    gflag++;
  200. X                }
  201. X                if (q=strtok(NULL,":"))
  202. X                if (!mflag) {
  203. X                    sscanf(q,"%o",&mode);
  204. X                    mflag++;
  205. X                }
  206. X            }
  207. X            }
  208. X            close(fd);
  209. X            if (!matchfound) {
  210. X            fprintf(stderr,"%s: '%s' not found in chall library.\n",
  211. X                argv[0],optarg);
  212. X            exit(2);
  213. X            }
  214. X        } else {
  215. X            fprintf(stderr,
  216. X                "%s: Unable to open ~/.chall library file.\n");
  217. X            exit(2);
  218. X        }
  219. X        break;
  220. X        case 'o':
  221. X        case 'u':
  222. X        owner = optarg;
  223. X        oflag++;
  224. X        break;
  225. X        case 'g':
  226. X        group = optarg;
  227. X        gflag++;
  228. X        break;
  229. X        case 'm':
  230. X        sscanf(optarg,"%o",&mode);
  231. X        mflag++;
  232. X        break;
  233. X        case 'd':
  234. X        if (dflag) errflag++;
  235. X        else {
  236. X            directory = optarg;
  237. X            dflag++;
  238. X        }
  239. X        break;
  240. X        default:
  241. X        errflag++;
  242. X    }
  243. X    if (errflag) {
  244. Xfprintf(stderr,"Illegal option.\n");
  245. Xfprintf(stderr,
  246. X"usage: %s [-o owner] [-g group] [-m mode] [-d directory] files ...\n",
  247. X    argv[0]);
  248. X    exit(1);
  249. X    }
  250. X
  251. X    if ( (oflag) && (!(Vowner=atoi(owner))) && ((p=getpwnam(owner))!=NULL) )
  252. X        Vowner = p->pw_uid;
  253. X
  254. X    if (gflag) {
  255. X    Vgroup = atoi(group);
  256. X    if (!Vgroup)
  257. X        if ((g=getgrnam(group))!=NULL)
  258. X        Vgroup = g->gr_gid;
  259. X        else 
  260. X        if ((p=getpwnam(group))!=NULL)
  261. X            Vgroup = p->pw_gid;
  262. X    }
  263. X
  264. X    f =  malloc(sizeof(struct stat));
  265. X    if (dflag) {
  266. X    strcpy(t,directory);
  267. X    tlen = strlen(t);
  268. X    t[tlen++]='/';
  269. X    }
  270. X    if ((oflag) || (gflag) || (mflag))
  271. X    for ( ; optind<argc; optind++) {
  272. X        if ( (dflag) && (argv[optind][0]!='/') ) {
  273. X        strcpy((t+tlen),argv[optind]);
  274. X        s = t;
  275. X        } else
  276. X        s = argv[optind];
  277. X        if (access(s,F_OK|W_OK)==0) {
  278. X        if ( (mflag) && (chmod(s,mode)==-1) )
  279. X            fprintf("Unable to chmod %s\n",s);
  280. X        sflag=1;
  281. X        if ( ((oflag) && (!gflag)) || ((!oflag) && (gflag)) ) {
  282. X            if (stat(s,f)!=-1) {
  283. X            OLDowner = (int) f->st_uid;
  284. X            OLDgroup = (int) f->st_gid;
  285. X            }
  286. X            else {
  287. X            fprintf(stderr,"stat of %s failed(%d)\n",
  288. X                s,errno); 
  289. X            sflag=0;
  290. X            }
  291. X        }
  292. X        if (sflag) {
  293. X            if ( (oflag) && (gflag) ) {
  294. X            if (chown(s,Vowner,Vgroup)==-1)
  295. X                fprintf(stderr,"Unable to chown/grp %s\n",
  296. X                    s);
  297. X            } else if (oflag) {
  298. X            if (chown(s,Vowner,OLDgroup)==-1)
  299. X                fprintf(stderr,"Unable to chown %s\n",
  300. X                    s);
  301. X            } else if (gflag)
  302. X            if (chown(s,OLDowner,Vgroup)==-1)
  303. X                fprintf(stderr,"Unable to chgrp %s\n",
  304. X                    s);
  305. X        }
  306. X        } else 
  307. X        fprintf(stderr,"access permission denied on file %s(%d)\n",
  308. X            s,errno);
  309. X    }
  310. X    free(f);
  311. X}
  312. END_OF_FILE
  313. if test 4143 -ne `wc -c <'chall.c'`; then
  314.     echo shar: \"'chall.c'\" unpacked with wrong size!
  315. fi
  316. # end of 'chall.c'
  317. fi
  318. if test -f 'chall.1' -a "${1}" != "-c" ; then 
  319.   echo shar: Will not clobber existing file \"'chall.1'\"
  320. else
  321. echo shar: Extracting \"'chall.1'\" \(2557 characters\)
  322. sed "s/^X//" >'chall.1' <<'END_OF_FILE'
  323. X.TH CHALL 1 09/26/88 "APT Technology, Inc."
  324. X.SH NAME
  325. Xchall \- a replacement for chmod, chgrp, and chmod
  326. X.SH SYNOPSIS
  327. X.B chall
  328. X[
  329. X.B \-o|-u \0owner
  330. X] [
  331. X.B \-g \0group
  332. X] [
  333. X.B \-m \0mode
  334. X] [
  335. X.B \-d\0directory
  336. X] [
  337. X.B \-x \0challdef
  338. X] files ...
  339. X.SH DESCRIPTION
  340. X.I chall
  341. Xis a replacement for the individual commands chmod, chown, and chgrp.
  342. X.PP
  343. XThe
  344. X.I chall
  345. Xprogram has the following five options:
  346. X.PP
  347. X.TP
  348. X.B \-o|-u owner
  349. XSpecifies the new owner to be applied to the list of files.
  350. XIf owner is not a number (uid),
  351. X.I /etc/passwd
  352. Xis searched to translate owner into a uid.
  353. X.I Chall
  354. Xalso allows the user to specify -u instead of -o if the user is more
  355. Xcomfortable with that notation.
  356. X.TP
  357. X.B \-g group
  358. XSpecifies the new group to be applied to the list of files. If group
  359. Xis not a number (gid), 
  360. X.I /etc/group
  361. Xis first searched to translate group into a gid.  If that fails
  362. X.I /etc/passwd
  363. Xis then searched to translate group into a gid.
  364. X.TP
  365. X.B \-m mode
  366. XSpecifies the new mode to be applied to the list of files. Note that
  367. Xthe +x -x +w -w +r -r options of some chmods is not support by
  368. X.I chall.
  369. X.TP
  370. X.B \-d directory
  371. X.I directory
  372. Xis prepended to all file names in the file list that do not begin
  373. Xwith
  374. X.I \/
  375. X(root).
  376. X.TP
  377. X.B \-x challdef
  378. X.I Chall
  379. Xsearches the file
  380. X.I $HOME/.chall
  381. Xlooking for a line which begins with challdef and uses the entries
  382. Xin the rest of the line as the owner, group, and mode to be applied
  383. Xby the 
  384. X.I Chall
  385. Xprogram.  Options fetched from the
  386. X.I $HOME/.chall
  387. Xfile are overridden by explict -o, -u, -g, or -m options in the
  388. Xargument line.
  389. X.SH DIAGNOSTICS
  390. XPermissions and access rights are checked at every operation. Any
  391. Xerrors will be reported on a per file basis.
  392. X.I Chall
  393. Xwill make all the changes possible skipping over errors when possible.
  394. X.SH IDENTIFICATION
  395. XAuthor: Brian Litzinger
  396. X.br
  397. XAPT Technology, Inc., San Jose, CA 95030
  398. X.sp 0
  399. X.SH FILES
  400. X$HOME/.chall
  401. X.SH ENVIROMENT
  402. XHOME
  403. X.SH SEE ALSO
  404. Xchmod(2), chown(2), chgrp(2)
  405. X.SH NOTES
  406. XThe
  407. X.I $HOME/.chall file is uses the following form, and can be
  408. Xcreated with any standard text editor:
  409. X.br
  410. X.ti +0.25i
  411. Xchalldef:owner:group:mode
  412. X.br
  413. XAn example
  414. X.I $HOME/.chall
  415. Xfile might be:
  416. X.br
  417. X.in +0.25i
  418. X.nf
  419. Xroot:root:sys:755
  420. Xbin:bin:bin:555
  421. Xrootuser:root::
  422. Xsysgroup::sys:
  423. Xme:brian::
  424. X.fi
  425. X.in -0.25i
  426. X.SH BUGS
  427. XThe +x -x +w -w +r -r syntax of some chmod commands in not supported
  428. Xby 
  429. X.I
  430. Xchall.
  431. X.br
  432. X.I chall
  433. Xcannot be applied to the executing image of itself as it will get
  434. Xa text file busy error (ETXTBSY) from the operating system.
  435. X.br
  436. XWhen using the -d option resultant file names must not exceed 255
  437. Xcharacters.
  438. END_OF_FILE
  439. if test 2557 -ne `wc -c <'chall.1'`; then
  440.     echo shar: \"'chall.1'\" unpacked with wrong size!
  441. fi
  442. # end of 'chall.1'
  443. fi
  444. echo shar: End of shell archive.
  445. exit 0
  446.  
  447.